package com.taoge.service.impl;

import com.taoge.entity.Employee;
import com.taoge.entity.EmployeeExample;
import com.taoge.repository.EmployeeMapper;
import com.taoge.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Employee> findAll() {
        return employeeMapper.selectByExampleWithDept(null);
    }

    @Override
    public void save(Employee employee) {
        employeeMapper.insertSelective(employee);
    }

    @Override
    public boolean findByName(String empName) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        int count = employeeMapper.countByExample(example);
        //存在返回false否则返回true
        return count==0;
    }

    @Override
    public Employee findById(Integer empId) {
        return employeeMapper.selectByPrimaryKey(empId);
    }

    @Override
    public void update(Employee employee) {
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    @Override
    public void deleteById(Integer empId) {
        employeeMapper.deleteByPrimaryKey(empId);
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        EmployeeExample example = new EmployeeExample();
        //delete from tb_emp where emp_id in (1,2,3)
        example.createCriteria().andEmpIdIn(ids);
        employeeMapper.deleteByExample(example);
    }
}
